//
//  CustomStarView.m
//  DebugDemo
//
//  Created by XFB on 2019/1/9.
//  Copyright © 2019年 ZhouCheng. All rights reserved.
//

#import "CustomStarView.h"

#define imageW 40

@interface CustomStarView ()

@property (nonatomic, strong) UIView *starNormalImageView;
@property (nonatomic, strong) UIView *starSelectImageView;

@end

@implementation CustomStarView

- (instancetype)initWithFrame:(CGRect)frame
              normalImageName:(NSString *)normalImageName
              selectImageName:(NSString *)selectImageName
{
    self = [super initWithFrame:frame];
    if (self) {
 
        self.grade = 0;
        
        self.starNormalImageView = [self setupStarViewWithImageName:normalImageName];
        self.starSelectImageView = [self setupStarViewWithImageName:selectImageName];
        [self addSubview:self.starNormalImageView];
        
        self.userInteractionEnabled = YES;
        self.clipsToBounds = YES;

        /// 单击手势
        UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImageView:)];
        [self addGestureRecognizer:tapRecognizer];
        /// 滑动手势
        UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(tapImageView:)];
        [self addGestureRecognizer:panRecognizer];
    }
    return self;
}

- (void)tapImageView:(UITapGestureRecognizer *)tapRecognizer
{
    CGPoint point = [tapRecognizer locationInView:self];
    
    if (point.x < 0) {
        point.x = 0;
    }
    
    NSLog(@"-----%.f", point.x);
    int x = (int) point.x / (imageW+5);
    NSLog(@"-----%d----", x);
    
    switch (x+1) {
        case 1:
        {
            self.grade = x+1;
            self.gradeString = @"1星，很不满意";
        }
            break;
        case 2:
        {
            self.grade = x+1;
            self.gradeString = @"2星，不满意";
        }
            break;
        case 3:
        {
            self.grade = x+1;
            self.gradeString = @"3星，一般";
        }
            break;
        case 4:
        {
            self.grade = x+1;
            self.gradeString = @"4星，满意";
        }
            break;
        case 5:
        {
            self.grade = x+1;
            self.gradeString = @"5星，非常满意";
        }
            case 6:
        {
            self.grade = 5;
            self.gradeString = @"5星，非常满意";
        }
            break;
        default:
            break;
    }
    
    if (self.gradeBlock) {
        self.gradeBlock(self.grade, self.gradeString);
    }
    self.starSelectImageView.frame = CGRectMake(0, 0, (x+1)*(imageW+5)-5, imageW);
    [self addSubview:self.starSelectImageView];
}

- (UIView *)setupStarViewWithImageName:(NSString *)imageName
{
    CGRect frame = self.bounds;
    
    UIView *view = [[UIView alloc] initWithFrame:frame];
    view.clipsToBounds = YES;
    
    for (NSInteger i = 0; i < 5; i ++) {
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];
        imageView.frame = CGRectMake(i*(imageW+5), 0, imageW, imageW);
        [view addSubview:imageView];
    }
    return view;
}

@end
